<?php
class IndexAction extends Action {
	
	function _initialize() {
	
	}
	
	function index() {
		$page = intval ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		
		$cacheName = getCacheName ( 'ask@action:index/page:' . $page . ";" );
		$data = S ( $cacheName );
		if (! $data) {
			$askModel = new AskModel ();
			$data ['seo'] ['title'] = '问题列表';
			$data ['seo'] ['keywords'] = '';
			$data ['seo'] ['description'] = '';
			$uid = '';
			$flag = 1;
			$sort = '';
			if (!empty($_REQUEST['sort'])) {
				if ($_REQUEST['sort'] == 5) {
					$flag = 0;
				}
				
				if ($_REQUEST['sort'] == 3) {
					$uid = $this->mid;
				}
				$sort = $_REQUEST['sort'];
			} 
			$data ['list'] = $askModel->listAsk($uid, $flag, $sort, @$_REQUEST['cid']);
			//$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题
			S ( $cacheName, $data, C ( 'cache_time_short' ) );
		}
		
		$data ['menu'] ['selected'] = 'index';
		$data['mid'] = $this->mid;
		global $ts;
		$ts ['site'] ['page_title'] = $data ['seo'] ['title'];
		$ts ['site'] ['site_header_keywords'] = $data ['seo'] ['keywords'];
		$ts ['site'] ['site_header_description'] = $data ['seo'] ['description'];
		
		$data['credit'] = X('Credit')->getUserCredit($this->mid);
		$this->data = $data;
		$this->display ();
	}	
	
	function loadContent() {
    	$page = (int) $_GET['page'];
    	$size = (int) $_GET['size'];
    	if (!empty($page) && !empty($size) && $page >1) {
			$askModel = new AskModel ();
			$uid = '';
			$flag = 1;
			$sort = '';
			if (!empty($_REQUEST['sort'])) {
				if ($_REQUEST['sort'] == 5) {
					$flag = 0;
				}
				
				if ($_REQUEST['sort'] == 3) {
					$uid = $this->mid;
				}
				$sort = $_REQUEST['sort'];
			} 
			
			$data ['list'] = $askModel->listAsk($uid, $flag, $sort, @$_REQUEST['cid']);
			if (!empty($data ['list'])) {
    			W('AskList', array('mid'=>$this->mid, 'list'=>$data['list']));
    		}			
    	}
	}
	
	function myask() {
		$flag = $_GET ['flag'];
		$page = intval ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		
		$cacheName = getCacheName ( 'ask@action:myask/page:' . $page . ";uid:" . $this->user ['uid'] . ";flag:{$flag};" );
		$data = S ( $cacheName );
		if (! $data) {
			$askModel = new AskModel ();
			$data ['seo'] ['title'] = '我的提问 ';
			if ($this->mid) {
				$data ['list'] = $askModel->listAsk ( $this->user ['uid'], $flag );
			}
			$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题
		}
		$data ['menu'] ['selected'] = 'myask';
		global $ts;
		$ts ['site'] ['page_title'] = $data ['seo'] ['title'];
		$ts ['site'] ['site_header_keywords'] = $data ['seo'] ['keywords'];
		$ts ['site'] ['site_header_description'] = $data ['seo'] ['description'];
		$this->data = $data;
		$this->display ();
	}
	function myanswer() {
		
		$page = intval ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		$cacheName = getCacheName ( 'ask@action:myanswer/page:' . $page . ";uid:" . $this->user ['uid'] . ";" );
		$data = S ( $cacheName );
		if (! $data) {
			$askModel = new AskModel ();
			$data ['seo'] ['title'] = '我的回答 ';
			if ($this->mid) {
				$data ['list'] = $askModel->listAnswer ( $this->user ['uid'] );
			}
			
			$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题                                        
		}
		$data ['menu'] ['selected'] = 'myanswer';
		global $ts;
		$ts ['site'] ['page_title'] = $data ['seo'] ['title'];
		$ts ['site'] ['site_header_keywords'] = $data ['seo'] ['keywords'];
		$ts ['site'] ['site_header_description'] = $data ['seo'] ['description'];
		$this->data = $data;
		$this->display ();
	}
	
	function search() {
		
		$page = intval ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		$key = trim ( $_GET ['key'] );
		$cacheName = getCacheName ( 'ask@search/key:' . $key . ';page:' . $page . ";" );
		$data = S ( $cacheName );
		if (! $data) {
			$askModel = new AskModel ();
			$data ['seo'] ['title'] = "搜索：{$key}的提问 ";
			$data ['seo'] ['keywords'] = '花卉提问，花卉求助，提问平台，花卉交流，花卉交友';
			$data ['seo'] ['description'] = '';
			$data ['list'] = $askModel->search ( $key );
			$data ['key'] = $key;
			$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题
			S ( $cacheName, $data, C ( 'cache_time_middle' ) );
		}
		$data ['menu'] ['selected'] = 'index';
		global $ts;
		$ts ['site'] ['page_title'] = $data ['seo'] ['title'];
		$ts ['site'] ['site_header_keywords'] = $data ['seo'] ['keywords'];
		$ts ['site'] ['site_header_description'] = $data ['seo'] ['description'];
		$this->data = $data;
		$this->display ();
	}
	
	function show() {
		$id = intval ( $_GET ['id'] );
		$cacheName = getCacheName ( 'ask@action:show/id:' . $id . ';' );
		$data = S ( $cacheName );
		if (! $data) {
			$askModel = new AskModel ();
			$data ['comment'] = $askModel->listComment ( $id );
			$data ['info'] = $askModel->getContext ( $id );
			$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题
			S ( $cacheName, $data, C ( 'cache_time_long' ) );
		}
		if ($data ['info'] ['ask_flag'] < 0) {
			$this->redirect ( 'ask/Index/index' );
		}
		global $ts;
		$ts ['site'] ['page_title'] = $data ['info'] ['ask_title'];
		$ts ['site'] ['site_header_keywords'] = $this->user ['uname'] . '提问，' . $data ['info'] ['ask_title'] . '，' . $this->user ['uname'];
		$ts ['site'] ['site_header_description'] = $data ['info'] ['ask_title'] . '：' . msubstr ( text ( $data ['info'] ['ask_context'] ), 0, 100 );
		$this->data = $data;
		$this->display ();
	
	}
	
	function ask() {
		
		if ($_POST && ! $_POST ['from']) {
			if (! $this->mid) {
				U ( 'home/Public/login', '', 1 );
			}
			/*if (! trim ( $_POST ['title'] )) {
				$this->error ( '标题必须写' );
			}*/
			if (! trim ( $_POST ['content'] )) {
				$this->error ( '内容必须写' );
			}
			$askContextModel = M ( 'ask_context' );
			$data = array ();
			
			$data ['user_name'] = $this->user ['uname'];
			$data ['user_id'] = $this->user ['uid'];
			$data ['ask_title'] = strip_tags ( substr($_POST ['context'], 0, 20) );
			$data ['ask_context'] = strip_tags ( str_replace ( "\r\n", '<br>', $_POST ['content'] ), '<br>,<p>' );
			$data ['ask_addtime'] = time ();
			$data ['ask_pay'] = $_POST ['pay'];
			$data ['ask_cid'] = $_POST ['cid'];
			$data ['ask_updatetime'] = time ();
			$id = $askContextModel->add ( $data );
			if (!$id) {
				echo 0;exit;
				//$this->error( '提交问题失败！' );
			}
			//动态
			service('MiniFeed')->put(array('ask_id'=>$id), 'ask');
			
			if ($id && $_FILES ['imgage1'] ['size']) {
				$askUploadModel = M ( 'ask_upload' );
				$images = X ( 'Xattach' )->upload ();
				foreach ( ( array ) $images ['info'] as $k => $v ) {
					$data = array ();
					$data ['upload_name'] = $v ['name'];
					$data ['context_id'] = $id;
					$data ['upload_path'] = $v ['savepath'] . $v ['savename'];
					$data ['upload_time'] = time ();
					$data ['add_time'] = time ();
					$data ['user_id'] = $this->mid;
					$askUploadModel->add ( $data );
				}
				$thumbUrl = str_replace ( '.', '_120_160.', $data ['upload_path'] );
				makeThumb ( 'data/uploads/' . $data ['upload_path'], 'data/uploads/' . $thumbUrl, 120, 160 );
				$typeData ['thumburl'] = $thumbUrl;
				$typeData ['thumbmiddleurl'] = $typeData ['picurl'] = $data ['upload_path'];
			}
			
			// 清除缓存：我回答的
			$cacheName = getCacheName ( 'ask@index/type:myask;page:1;uid:' . $this->user ['uid'] . ';' );
			S ( $cacheName, NULL );
			
			$cacheName = getCacheName ( "ask@model:listask/page:1;uid:{$this->mid};flag:1;" );
			S ( $cacheName, NULL );
			$url = U ( 'ask/Index/index');
			$this->jumpUrl = $url;
			$askModel = new AskModel ();
			$askModel->addWeibo ( strip_tags ( $_POST ['title'] ), $url, $this->uid, msubstr ( strip_tags ( $_POST ['context'] ), 0, 200 ), $typeData );
			echo 1;
			//$this->success ( '问题已经提交，请随时关注网友的回答！' );
		} else {
			echo 0;
			//$data ['seo'] ['title'] = '我要提问 ';
		}
		/*$askModel = new AskModel ();
		$data ['hotAsk'] = $askModel->listAsk ( 0, 1, 1 ); // 推荐问题
		global $ts;
		$ts ['site'] ['page_title'] = $data ['seo'] ['title'];
		$ts ['site'] ['site_header_keywords'] = $data ['seo'] ['keywords'];
		$ts ['site'] ['site_header_ddescription'] = $data ['seo'] ['description'];
		$data ['menu'] ['selected'] = 'addask';
		$this->data = $data;
		$this->display ();*/
	}
	
	function addViewNum($id) {
		$askModel = D ( 'ask_context' );
		$id = intval ( $_REQUEST ['id'] );
		if ($askModel->setInc ( 'view_num', 'ask_id=' . $id, '1' )) {
			echo 1;
		} else {
			echo 0;
		}
	}

}

?>